package com.wk.springboot.httpUtils;

import java.io.File;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;

/**
 * @Classname MultipartEntityBuilderWraper
 * @Description 对MultipartEntityBuilder 进行简单包装，避免代码直接依赖第三方
 * @Date 2020/8/7 11:15
 * @Author by weikai
 */
public final class MultipartEntityBuilderWraper {

    private MultipartEntityBuilder builder;

    private MultipartEntityBuilderWraper() {
        builder = MultipartEntityBuilder.create();
    }

    public static MultipartEntityBuilderWraper create() {
        return new MultipartEntityBuilderWraper();
    }

    public MultipartEntityBuilderWraper addTextBody(final String name, final String text, final ContentType contentType) {
        this.builder.addTextBody(name, text, contentType);
        return this;
    }

    public MultipartEntityBuilderWraper addTextBody(final String name, final String text) {
        this.builder.addTextBody(name, text);
        return this;
    }

    /**
     * 添加文件参数
     * @param name 参数名
     * @param file 文件
     * @param contentType 内容类型
     * @param filename 文件名
     * @return 当前对象
     */
    public MultipartEntityBuilderWraper addBinaryBody(final String name, final File file, final ContentType contentType,
            final String filename) {
        this.builder.addBinaryBody(name, file, contentType, filename);
        return this;
    }

    /**
     * 添加流数据
     * @param name  参数名
     * @param stream  流
     * @param contentType 内容类型
     * @param filename 文件名
     * @return 当前对象
     */
    public MultipartEntityBuilderWraper addBinaryBody(final String name, final InputStream stream, final ContentType contentType,
            final String filename) {
        this.builder.addBinaryBody(name, stream, contentType, filename);
        return this;
    }

    /**
     * 添加流数据，默认application/octet-stream
     * @param name  参数名
     * @param stream 流
     * @return 当前对象
     */
    public MultipartEntityBuilderWraper addBinaryBody(final String name, final InputStream stream) {
        this.builder.addBinaryBody(name, stream);
        return this;
    }

    public HttpEntity build() {
        return this.builder.build();
    }

}
